home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / (Samples) / Text Display / Code / TMyApp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-23  |  3.6 KB  |  197 lines

  1. /*    TMyApp.cpp
  2.  *
  3.  *        Create my application
  4.  */
  5.  
  6. #include <XWindow.h>
  7. #include <XStdControls.h>
  8. #include <XPrinter.h>
  9. #include "MyApp.h"
  10.  
  11. /************************************************************************/
  12. /*                                                                        */
  13. /*    Construction/Destruction                                            */
  14. /*                                                                        */
  15. /************************************************************************/
  16.  
  17. /*    TMyApp::TMyApp
  18.  *
  19.  *        Bring me up. Initialize the standard controls library
  20.  */
  21.  
  22. TMyApp::TMyApp()
  23. {
  24.     XGInitStandardControls();
  25. }
  26.  
  27. /*    TMyApp::~TMyApp
  28.  *
  29.  *        Shut me down. Shut down standard control library
  30.  */
  31.  
  32. TMyApp::~TMyApp()
  33. {
  34.     XGKillStandardControls();
  35. }
  36.  
  37. /************************************************************************/
  38. /*                                                                        */
  39. /*    Message Dispatch                                                    */
  40. /*                                                                        */
  41. /************************************************************************/
  42.  
  43. /*    TMyApp::ReceiveDispatch
  44.  *
  45.  *        When a menu item is selected, a message is dispached. I receive
  46.  *    all those not handled elsewhere
  47.  */
  48.  
  49. long TMyApp::ReceiveDispatch(long msg, long arg, void *parg)
  50. {
  51.     XGSMenuStatusRecord *s;
  52.     
  53.     switch (msg) {
  54.         case KEventDoMenuCommand:
  55.             /*
  56.              *    Do the 'open' and 'page setup'
  57.              */
  58.             
  59.             switch (arg) {
  60.                 case 100:            /* Open */
  61.                     DoOpenWindow();
  62.                     return 1;
  63.                 case 101:            /* Page Setup */
  64.                     GPrinter.PageSetup();
  65.                     return 1;
  66.             }
  67.             break;
  68.             
  69.         case KEventGetMenuStatus:
  70.             /*
  71.              *    Enable 'open' and 'page setup'
  72.              */
  73.             
  74.             switch (arg) {
  75.                 case 100:
  76.                 case 101:
  77.                     s = (XGSMenuStatusRecord *)parg;
  78.                     s->enable = true;
  79.                     return 1;
  80.             }
  81.             break;
  82.     }
  83.     
  84.     /*
  85.      *    Default: pass up
  86.      */
  87.     
  88.     return XGAppMultiWindow::ReceiveDispatch(msg,arg,parg);
  89. }
  90.  
  91. /*    TMyApp::CreateView
  92.  *
  93.  *        Create my custom views
  94.  */
  95.  
  96. XGView *TMyApp::CreateView(long viewID, XGView *parent, XGArgStream &s)
  97. {
  98.     switch (viewID) {
  99.         case 'TEXT':
  100.             return new TTextView(parent,s);
  101.         default:
  102.             return NULL;
  103.     }
  104. }
  105.  
  106. /************************************************************************/
  107. /*                                                                        */
  108. /*    Window management                                                    */
  109. /*                                                                        */
  110. /************************************************************************/
  111.  
  112. /*    TMyApp::DoOpenWindow
  113.  *
  114.  *        Open a window
  115.  */
  116.  
  117. void TMyApp::DoOpenWindow(void)
  118. {
  119.     XGWindow *w;
  120.     TTextView *t;
  121.     FILE *f;
  122.     
  123.     /*
  124.      *    Open the file
  125.      */
  126.  
  127. #if OPT_MACOS == 1
  128.     StandardFileReply reply;
  129.     SFTypeList list = { 'TEXT' };
  130.     short refNum;
  131.     long dirID;
  132.     
  133.     StandardGetFile(NULL,1,list,&reply);
  134.     if (!reply.sfGood) return;
  135.     HGetVol(NULL,&refNum,&dirID);
  136.     HSetVol(NULL,reply.sfFile.vRefNum,reply.sfFile.parID);
  137.     f = fopen(p2cstr(reply.sfFile.name),"r");
  138.     HSetVol(NULL,refNum,dirID);
  139. #endif
  140.  
  141. #if OPT_WINOS == 1
  142.     OPENFILENAME ofn;
  143.     char filter[256];
  144.     char *c;
  145.     char title[256];
  146.     char file[256];
  147.     
  148.     memset(&ofn,0,sizeof(ofn));
  149.     ofn.lStructSize = sizeof(ofn);
  150.     ofn.hwndOwner = _GMainWindow;
  151.     ofn.hInstance = _GInstance;
  152.     
  153.     strcpy(filter,"Text Files(*.TXT)|*.txt|");
  154.     for (c = filter; *c != 0; c++) if (*c == '|') *c = 0;
  155.     ofn.lpstrFilter = filter;
  156.     file[0] = 0;
  157.     title[0] = 0;
  158.     
  159.     ofn.nFilterIndex = 1;
  160.     ofn.lpstrFile = file;
  161.     ofn.nMaxFile = sizeof(file);
  162.     ofn.lpstrFileTitle = title;
  163.     ofn.nMaxFileTitle = sizeof(title);
  164.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  165.     
  166.     if (!GetOpenFileName(&ofn)) return;
  167.     
  168.     f = fopen(ofn.lpstrFile,"r");
  169. #endif
  170.     
  171.     /*
  172.      *    Create the text window
  173.      */
  174.     
  175.     w = XGWindow::Create(128);                    // text window
  176.     
  177.     /*
  178.      *    Name the window
  179.      */
  180.     
  181. #if OPT_MACOS == 1
  182.     w->SetWindowName((char *)reply.sfFile.name);
  183. #endif
  184. #if OPT_WINOS == 1
  185.     w->SetWindowName(ofn.lpstrFileTitle);
  186. #endif
  187.  
  188.     /*
  189.      *    Open the text file
  190.      */
  191.     
  192.     t = (TTextView *)(w->FindViewByID(101));
  193.     if (t) {
  194.         t->ShowFile(f);
  195.     }
  196. }
  197.